﻿using System;
using System.Linq;
using System.Web.UI.WebControls;

namespace ECommerce
{
	public partial class OrderDetail : BasePage
	{
		private Service.IOrderService _OrderService;
		private Service.IProductService _ProductService;
		private Service.IShippingMethodService _ShippingMethodService;

		private Data.Entity.Order _TheOrder;

		public int OrderID
		{
			get
			{
				return CommonHelper.QueryStringInt("orderid");
			}
		}

		public Data.Entity.Order TheOrder
		{
			get
			{
				if (OrderID <= 0)
				{
					return null;
				}

				if (_TheOrder != null)
				{
					return _TheOrder;
				}

				if (_OrderService == null)
				{
					_OrderService = new Service.OrderService();
				}

				_TheOrder = _OrderService.GetTheOrderByOrderID(OrderID);

				return _TheOrder;
			}
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
				BindOrderNote();

				BindOrder();
			}
		}

		protected void Page_PreRender(object sender, EventArgs e)
		{
			this.Title = GetLocalResourceString("PageTitle.OrderDetail");
			this.LOrderNumberLabel.Text = GetLocalResourceString("OrderDetail.OrderNumber.LabelText");
			this.LOrderNoteTitle.Text = GetLocalResourceString("OrderDetail.Item.OrderNote.Title");
			this.LOrderProductTitle.Text = GetLocalResourceString("OrderDetail.Item.OrderProduct.Title");
			this.LOrderTotalPriceLabel.Text = GetLocalResourceString("OrderDetail.OrderTotalPrice.Label");
			this.LShippingAddressTitle.Text = GetLocalResourceString("OrderDetail.ShippingAddress.Title");
			this.LShippingAddressFullNameLabel.Text = GetLocalResourceString("OrderDetail.ShippingAddress.FullName.Label");
			this.LShippingAddressAreaLabel.Text = GetLocalResourceString("OrderDetail.ShippingAddress.Area.Label");
			this.LShippingAddressPhoneNumberLabel.Text = GetLocalResourceString("OrderDetail.ShippingAddress.PhoneNumber.Label");
			this.LShippingAddressMobileNumberLabel.Text = GetLocalResourceString("OrderDetail.ShippingAddress.MobileNumber.Label");
			this.LShippingAddressAddressDetailLabel.Text = GetLocalResourceString("OrderDetail.ShippingAddress.AddressDetail.Label");
			this.LShippingMethodTitle.Text = GetLocalResourceString("OrderDetail.ShippingMethod.Title");
			this.LShippingMethodHeader.Text = GetLocalResourceString("OrderDetail.ShippingMethod.Title");
			this.LShippingCostHeader.Text = GetLocalResourceString("OrderDetail.ShippingMethod.ShippingCostHeader");
			this.LOrderDetailDeliveryTitle.Text = GetLocalResourceString("OrderDetail.Delivery.Title");
			this.LOrderDetailDeliveryCompanyHeader.Text = GetLocalResourceString("OrderDetail.Delivery.CompanyHeader");
			this.LOrderDetailDeliveryTrackingNumberHeader.Text = GetLocalResourceString("OrderDetail.Delivery.TrackingNumberHeader");
			this.LOrderDetailDeliveryDateHeader.Text = GetLocalResourceString("OrderDetail.Delivery.DeliveryDateHeader");
			this.LOrderDetailPaymentTitle.Text = GetLocalResourceString("OrderDetail.Payment.Title");
			this.LOrderSummaryTitle.Text = GetLocalResourceString("OrderDetail.OrderSummary.Title");
			this.LOrderSummaryProdTotalPriceLabel.Text = GetLocalResourceString("OrderDetail.OrderSummary.ProductTotalPrice.Label");
			this.LOrderSummaryShippingCostLabel.Text = GetLocalResourceString("OrderDetail.OrderSummary.ShippingCost.Label");
			this.LOrderSummaryCashTicketCostLabel.Text = GetLocalResourceString("OrderDetail.OrderSummary.CashTicketCost.Label");
			this.LOrderSummaryToPay.Text = GetLocalResourceString("OrderDetail.OrderSummary.ToPay");
		}

		protected void ListProducts_PreRender(object sender, EventArgs e)
		{
			if (ListProducts.Items.Count > 0)
			{
				var LiteralProductNameHeader = ListProducts.FindControl("LiteralProductNameHeader") as Literal;
				var LiteralProductPriceHeader = ListProducts.FindControl("LiteralProductPriceHeader") as Literal;
				var LiteralProductQuantityHeader = ListProducts.FindControl("LiteralProductQuantityHeader") as Literal;
				var LitieralProductSumPriceHeader = ListProducts.FindControl("LitieralProductSumPriceHeader") as Literal;

				LiteralProductNameHeader.Text = GetLocalResourceString("OrderDetail.ProductList.ProductName.HeadText");
				LiteralProductPriceHeader.Text = GetLocalResourceString("OrderDetail.ProductList.Price.HeaderText");
				LiteralProductQuantityHeader.Text = GetLocalResourceString("OrderDetail.ProductList.Quantity.HeaderText");
				LitieralProductSumPriceHeader.Text = GetLocalResourceString("OrderDetail.ProductList.SumPrice.HeaderText");
			}
		}

		private void BindOrder()
		{
			if (TheOrder != null)
			{
				this.Literal_DeliveryCompany.Text = TheOrder.ShippingCompany;
				this.Literal_DeliveryDate.Text = TheOrder.DeliveryDate.HasValue ? TheOrder.DeliveryDate.Value.ToString() : string.Empty;
				this.Literal_DeliveryTrackingNumber.Text = TheOrder.TrackingNumber;
				this.Literal_PaymentMethod.Text = TheOrder.PaymentMethodName;
				this.Literal_ShippingAddress_AddressDetail.Text = TheOrder.ShippingAddress1;
				this.Literal_ShippingAddress_Area.Text = TheOrder.ShippingCountry + TheOrder.ShippingStateProvince + TheOrder.ShippingCity;
				this.Literal_ShippingAddress_FullName.Text = TheOrder.ShippingFirstName + TheOrder.ShippingLastName;
				this.Literal_ShippingAddress_MobileNumber.Text = TheOrder.ShippingMobileNumber;
				this.Literal_ShippingAddress_PhoneNumber.Text = TheOrder.ShippingPhoneNumber;
				var shippingCost = GetShippingMethodCost(TheOrder.ShippingStateProvinceID.Value, TheOrder.ShippingMethodID.Value);
				this.Literal_ShippingCost.Text = shippingCost.ToString("c2");
				this.Literal_ShippingMethod.Text = GetLocalResourceString("ShippingMethod." + TheOrder.ShippingMethod);
				this.LiteralOrderNumber.Text = TheOrder.OrderNumber;
				this.LiteralShippingCost.Text = shippingCost.ToString("c2");
				this.LiteralCashTicketCost.Text = TheOrder.OrderDiscount.HasValue ? TheOrder.OrderDiscount.Value.ToString("c2") : string.Empty;

				var productTotalPrice = BindProductData();
				this.LiteralToPay.Text = TheOrder.OrderTotal.Value.ToString("c2");
			}
		}

		private decimal GetShippingMethodCost(int stateProvinceID, int shippingMethodID)
		{
			if (stateProvinceID > 0 && shippingMethodID > 0)
			{
				if (_ShippingMethodService == null)
				{
					_ShippingMethodService = new Service.ShippingMethodService();
				}

				var shippingCosts = _ShippingMethodService.GetShippingByStateAndProvinceByProvince(stateProvinceID);

				if (shippingCosts != null)
				{
					var cost = (from s in shippingCosts
								where s.ShippingMethodID.Equals(shippingMethodID)
								select s.TransportCosts).FirstOrDefault();

					return cost;
				}
				else
				{
					return decimal.Zero;
				}
			}
			else
			{
				return decimal.Zero;
			}
		}

		private decimal BindProductData()
		{
			if (_OrderService == null)
			{
				_OrderService = new Service.OrderService();
			}

			if (_ProductService == null)
			{
				_ProductService = new Service.ProductService();
			}

			var orderProductMappings = (from op in _OrderService.GetOrderProductMappingByOrder(OrderID)
										join prod in _ProductService.GetProductsByLanguage(TheOrder.UserLanguageID) on op.ProductID equals prod.ProductID
										select new
										{
											ProductID = prod.ProductID,
											ProductName = prod.Name,
											ListPrice = prod.ListPrice,
											Quantity = op.Quantity,
											SumPrice = prod.ListPrice * op.Quantity
										});

			if (orderProductMappings != null && orderProductMappings.Count() > 0)
			{
				//var lstOrderProductMappings = orderProductMappings.ToList();

				//if (lstOrderProductMappings.Count > 0)
				//{
				ListProducts.DataSource = orderProductMappings;
				ListProducts.DataBind();

				var sumProductPrice = orderProductMappings.Sum(e => e.SumPrice);

				LiteralOrderTotalPrice.Text = sumProductPrice.Value.ToString("c2");
				LiteralProductTotalPrice.Text = sumProductPrice.Value.ToString("c2");

				return sumProductPrice.Value;
				//}
			}

			return decimal.Zero;
		}

		private void BindOrderNote()
		{
			if (OrderID > 0)
			{
				if (_OrderService == null)
				{
					_OrderService = new Service.OrderService();
				}

				ListViewOrderNote.DataSource = _OrderService.GetOrderNoteByOrder(OrderID);
				ListViewOrderNote.DataBind();
			}
		}
	}
}
